/**
 * 
 */
package com.pulsarwebdevelopment.concordance.plugins.android;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import com.pulsarwebdevelopment.concordance.plugins.FileType;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author Matthew Dickinson
 *
 */
public class FileBrowser extends LinearLayout {

	private static final String[] EMPTY_STR_ARR = new String[0];
	public final static String FILE_PATH = "/sdcard/quiz-concordance/";
	
	EditText filePathInput;
	String selectedFile;
	FileType type;
	Spinner fileChooser;
	
	
	public FileBrowser(Context context) {
		this(context, FileType.TEXT);
	}
	
	public FileBrowser(Context context, FileType fileType) {
		super(context);
		
		initialize(fileType);
	}

	public FileBrowser(Context context, AttributeSet attrs) {
        this(context, attrs,FileType.TEXT);
	}
	
	public FileBrowser(Context context, AttributeSet attrs, FileType fileType) {
        super(context, attrs);
        
        initialize(fileType);
	}
	
	private void initialize(FileType fileType){
		this.type = fileType;
		
		//allow a user to hand create a file
		filePathInput = new EditText(this.getContext());
		filePathInput.setWidth(350);
		
		this.filePathInput.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				FileBrowser.this.selectedFile = s.toString();//FileBrowser.this.filePathInput.getText().toString());
				//Toast.makeText(getContext(), s.toString(), 100);
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {}
			
			public void afterTextChanged(Editable s) {}
		});
		
		this.addView(filePathInput);
		
		
		//set up a way for a user to choose and existing file
		fileChooser = new Spinner(this.getContext());
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
											this.getContext(), android.R.layout.simple_spinner_item);
		
		for(String file : this.getFiles()){
			adapter.add(file);
		}
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		fileChooser.setAdapter(adapter);
		fileChooser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> spinner, View arg1,
					int arg2, long arg3) {
				FileBrowser.this.setSelectedFile(new File(spinner.getSelectedItem().toString()));
				
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		this.addView(fileChooser);
	}
	
	private String[] getFiles(){
		
		//return new String[]{"test1.txt", "face.txt", "blargh.txt"};
		//List<String> files = new ArrayList<String>();
		String[] files = EMPTY_STR_ARR;
		
		File dir = new File(FILE_PATH);
		
		
		if(dir.isDirectory()){
			//(String filename : dir.list(this.type.getFilter())){
			
			files = dir.list(this.type.getFilter());
		} else if(!dir.exists()){
			dir.mkdir();
			
			//return no files (since I just created the directory)
		} else {
			AlertDialog.Builder diag = new AlertDialog.Builder(this.getContext());
			diag.setMessage("There is already a file on the SD card with the name 'quiz-concordance. " +
					"This directory name is needed for by application to store files");
			diag.setCancelable(false);
			diag.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
			diag.create();
			
		}
		
		return files;
	}
	
	public void setSelectedFile(String newFilePath){
		this.setSelectedFile(new File(newFilePath));
	}
	
	public void setSelectedFile(File newFile){
		//this.selectedFile = newFile;
		
		this.filePathInput.setText(newFile.getName().toString());
		//simply set the text on the input, the OnTextChanged will change the actual value
	}
	
	public File getSelectedFile(){
		return new File(FILE_PATH, this.selectedFile);
	}
	
	public void setType(FileType newType){
		this.type = newType;
	}
	

}

